package dbExec

import (
	"database/sql"
	"db2s/global"
	"reflect"
	"time"
)

type IndexColumnStruct struct {
	Drivce              string
	Schema              string `json:"schema"`
	Table               string `json:"table"`
	TmpTableFileName    string
	DBType              string
	ColumnName          []string            `json:"columnName"`
	TableColumn         []map[string]string `json:"tableColumn"`
	Sqlwhere            string
	ColData             []map[string]string
	SelectColumn        map[string]string
	QuerySource         string
	ShardName           string            `json:"shardName"`
	PartitionId         string            `json:"partitionId"`
	IndexCilumnDataType string            `json:"indexColumnDataType"`
	RowsDisposMapList   map[string]string `json:"rowsDisposmapList"`
	HotSqlBeginTime     string
	HostSqlEndTime      string
	SqlExecStopTime     time.Duration `json:"sqlExecStopTime"`
	DB                  *sql.DB
	MissIndexTablePx    string `json:"missIndexTablePx"`
	FixRows             int64  `json:"fixRows"`
	Sqlmode             bool
	Eas                 global.EncryptionProperties
	Scn                 string
	PartitionName       string
}
type ColumnMetaEr interface {
	TypeJudge(cn []string, logseq int64) (Type []string)
}

type IndexColumnPartValue interface {
	TmpIndexColumnStartPartValue1(s global.IndexColumnStartPartValueP) (EndRows map[string]interface{}, err error)
	TmpColumnDataTypeJudge(cn []string, logseq int64) (Type []string)
	TmpGeneratingQuerySql(s global.IndexColumnStartPartValueP) (string, error)
}
type TableIndexColumner interface {
	IndexColumnStartPartValue1(db map[string]*sql.DB, where map[string]string, Limitseq int64, selectColumn string, logThreadSeq int64) (map[string]interface{}, error)
	IndexColumnPartValue
	ColumnDataTypeJudge(cn string, logseq int64) (Type string)
	ColumnvalueConvert(TableColumn []map[string]string, cn, s string, logseq int64) (d string)
	TableRows(db map[string]*sql.DB, logThreadSeq int64) (uint64, error)
	TableCount(db *sql.DB, logThreadSeq int64) (uint64, error)
	GetHotSqlVariable(db map[string]*sql.DB, sqlId string, logThreadSeq int64) ([]map[string]interface{}, error)
	GetHotSqlElaptimePerms(db map[string]*sql.DB, logThreadSeq int64) ([]map[string]interface{}, error)
	GetHotSqlText(db map[string]*sql.DB, logThreadSeq int64) ([]map[string]interface{}, error)
	HotSqlPlayBack(db map[string]*sql.DB, strsql string, logThreadSeq int64) map[string]string
	QueryTableIndexColumnInfo(db *sql.DB, schema string, table []string) ([]global.IndexColumn, error)
	IndexDisposF(queryData []map[string]interface{}, logThreadSeq int64) (map[string][]string, map[string][]string, map[string][]string)
	NoIndexGeneratingQueryCriteria(db map[string]*sql.DB, logThreadSeq int64) (sc chan *string, err error)
	GeneratingQuerySql(where map[string]string, logThreadSeq int64) (string, error)
	IndexColumnPartQuryGroupCount1(db map[string]*sql.DB, where map[string]string, selectColumn string, logThreadSeq int64) (D int64, err error)
	GeneratingEncryptionQuerySql(sqlS string, EncryptionAlgorithm string, logThreadSeq int64) (selectSql string)
	GeneratingQueryCriteria(db map[string]*sql.DB, sqlwhere string, logThreadSeq int64) (any, error)
	TableTruncate(db map[string]*sql.DB, logThreadSeq int64) bool
	CountIndexCheck(db map[string]*sql.DB) (dd uint64, mqSwitch bool)
}

func (qticis IndexColumnStruct) TableIndexColumn() (t TableIndexColumner) {
	mutex.Lock()
	defer mutex.Unlock()
	if v1, ok := dbTypeInterFuncRef[qticis.DBType]; ok {
		if v1.Kind() == reflect.Func {
			args := []reflect.Value{
				reflect.ValueOf(qticis),
				reflect.ValueOf(tableDataAPI[qticis.DBType]),
			}
			i := v1.Call(args)
			for ii := 0; ii < len(i); ii++ {
				t = i[ii].Interface().(TableIndexColumner)
			}
		}
	}
	return
}
func TQuery(qticis IndexColumnStruct) (t TableIndexColumner) {
	//if strings.EqualFold(qticis.DBType, "mysql") {
	//	t = &mysql.QueryTable{
	//		Schema:            qticis.Schema,
	//		Table:             qticis.Table,
	//		ColumnName:        qticis.ColumnName,
	//		TableColumn:       qticis.TableColumn,
	//		RowsActiveMapList: qticis.RowsActiveMapList,
	//		SqlExecStopTime:   qticis.SqlExecStopTime,
	//		MissIndexTablePx:  qticis.MissIndexTablePx,
	//		SqlMode:           qticis.SqlMode,
	//		Eas:               qticis.Eas,
	//		Scn:               qticis.Scn,
	//		PartitionName:     qticis.PartitionName,
	//		PartitionId:       qticis.PartitionId,
	//	}
	//}
	//if strings.EqualFold(qticis.DBType, "oracle") {
	//	t = &oracle.QueryTable{
	//		Schema:            qticis.Schema,
	//		Table:             qticis.Table,
	//		ColumnName:        qticis.ColumnName,
	//		TableColumn:       qticis.TableColumn,
	//		RowsActiveMapList: qticis.RowsActiveMapList,
	//		HotSqlBeginTime:   qticis.HotSqlBeginTime,
	//		HostSqlEndTime:    qticis.HostSqlEndTime,
	//		MissIndexTablePx:  qticis.MissIndexTablePx,
	//		Eas:               qticis.Eas,
	//		Scn:               qticis.Scn,
	//		PartitionName:     qticis.PartitionName,
	//		PartitionId:       qticis.PartitionId,
	//	}
	//}
	//if strings.EqualFold(qticis.DBType, "cluster") {
	//	t = &cluster.QueryTable{
	//		Schema:              qticis.Schema,
	//		Table:               qticis.Table,
	//		ColumnName:          qticis.ColumnName,
	//		TableColumn:         qticis.TableColumn,
	//		ShardName:           qticis.ShardName,
	//		IndexColumnDataType: qticis.IndexCilumnDataType,
	//		RowsActiveMapList:   qticis.RowsActiveMapList,
	//		SqlExecStopTime:     qticis.SqlExecStopTime,
	//		MissIndexTablePx:    qticis.MissIndexTablePx,
	//		SqlMode:             qticis.SqlMode,
	//		Eas:                 qticis.Eas,
	//		Scn:                 qticis.Scn,
	//		PartitionName:       qticis.PartitionName,
	//		PartitionId:         qticis.PartitionId,
	//	}
	//}
	return
}
